package com.fr.report.script;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.report.script.core.parser.AtomColumnIndex;
import com.fr.report.script.core.parser.ColumnRowRange;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SingleRowNameSpace
  implements NameSpace
{
  private TableData tableData;
  private int rowIndex;
  private Object currentValue;
  private transient Map columnNameIndexMap = null;

  public SingleRowNameSpace(TableData paramTableData, int paramInt, Object paramObject)
  {
    this.tableData = paramTableData;
    this.rowIndex = paramInt;
    this.currentValue = paramObject;
  }

  public void setRowIndex(int paramInt)
  {
    this.rowIndex = paramInt;
  }

  public void setCurrentValue(Object paramObject)
  {
    this.currentValue = paramObject;
  }

  public Function getMethod(Object paramObject, Calculator paramCalculator)
  {
    return null;
  }

  public Object getCellElementAttribute(ColumnRow paramColumnRow, Object paramObject, Calculator paramCalculator)
  {
    return null;
  }

  public Object getVariable(Object paramObject, Calculator paramCalculator)
  {
    if ("$$$".equals(paramObject))
      return this.currentValue;
    if (paramObject instanceof ColumnRowRange)
    {
      paramObject = ((ColumnRowRange)paramObject).toString();
      if (!(((String)paramObject).matches("^[a-zA-Z]+\\d+$")))
        return null;
    }
    if ((!(paramObject instanceof String)) || (this.tableData == null) || (this.rowIndex < 0))
      return null;
    int i = getColumnIndex(paramObject);
    return DataUtils.getTableDataValue(this.tableData, this.rowIndex, i);
  }

  private int getColumnIndex(Object paramObject)
  {
    if (paramObject instanceof AtomColumnIndex)
      return (((AtomColumnIndex)paramObject).getIndex() - 1);
    if (this.columnNameIndexMap == null)
    {
      this.columnNameIndexMap = new HashMap();
      try
      {
        int i = 0;
        int j = this.tableData.getColumnCount();
        while (i < j)
        {
          this.columnNameIndexMap.put(this.tableData.getColumnName(i), new Integer(i));
          ++i;
        }
      }
      catch (TableDataException localTableDataException)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
      }
    }
    String str = (String)paramObject;
    Integer localInteger = (Integer)this.columnNameIndexMap.get(str);
    if ((localInteger == null) && (str.startsWith("$")))
      localInteger = (Integer)this.columnNameIndexMap.get(str.substring(1));
    return ((localInteger == null) ? -2147483648 : localInteger.intValue());
  }
}